import 'package:async/async.dart';
import 'package:boss/models/staff.dart';
import 'package:boss/provider/staff_provider.dart';
import 'package:boss/ui/page/staff/page/staffDetail/staff_detail_page.dart';
import 'package:boss/ui/page/staff/widget/staff_item.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class StaffList extends StatelessWidget {
  Future<Result<List<Staff>>> loadTask(BuildContext context) async {
    var staffProvider = Provider.of<StaffProvider>(
      context,
      listen: false,
    );
    return staffProvider.loadMoreStaffList();
  }

  Future<Result<List<Staff>>> refreashTask(BuildContext context) async {
    var staffProvider = Provider.of<StaffProvider>(
      context,
      listen: false,
    );
    return staffProvider.refreshStaffList();
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Consumer<StaffProvider>(
        builder: (context, staffProvider, child) {
          return LoaderPullRefresh<Staff>(
            key: staffProvider.loaderPullRefreshKey,
            loadTask: () => loadTask(context),
            refreshTask: () => refreashTask(context),
            builder: (context) {
              if (staffProvider.staffs.length == 0) {
                return EmptyWidget();
              }
              return ListView.builder(
                padding: EdgeInsets.symmetric(
                  horizontal: 30.w,
                ),
                itemBuilder: (context, index) {
                  var current = staffProvider.staffs[index];

                  return StaffItem(
                    data: current,
                    onTap: () {
                      Navigator.pushNamed(context, StaffDetailPage.pageName, arguments: {
                        'staff': Staff.fromJson(current.toJson()),
                      });
                    },
                  );
                },
                itemCount: staffProvider.staffs.length,
              );
            },
          );
        },
      ),
    );
  }
}
